package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.OldInConstant;
import com.csun.cmny.provider.model.domain.OmcFollowUp;
import com.csun.cmny.provider.model.domain.OmcFollowUpLog;
import com.csun.cmny.provider.model.dto.FollowUpSelectDto;
import com.csun.cmny.provider.model.dto.followUp.FollowUpShiftDto;
import com.csun.cmny.provider.model.dto.revise.FollowUpDto;
import com.csun.cmny.provider.model.vo.RpcRoleVo;
import com.csun.cmny.provider.model.vo.followUp.FollowUpVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFollowUpLogService;
import com.csun.cmny.provider.service.OmcFollowUpService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/followUp", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcFollowUpController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFollowUpService omcFollowUpService;
    @Resource
    private OmcFollowUpLogService omcFollowUpLogService;
    @Resource
    private UacRpcService uacRpcService;



    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "回访跟进")
    @RequestMapping(value = "/getFollowUp", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid FollowUpDto followUpDto, BindingResult br) {


        OmcFollowUp omcFollowUp = omcFollowUpService.selectByKey(followUpDto.getId());
        OmcFollowUpLog omcFollowUpLog = new OmcFollowUpLog();
        if(PublicUtil.isNotEmpty(omcFollowUp)){

            //更新客户跟进表信息
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            omcFollowUp.setReviewStaff(getLoginAuthDto().getUserName());
            omcFollowUp.setFollowUpDate(sdf.format(new Date()));
            omcFollowUp.setIntention(followUpDto.getIntention());


            //新增客户跟进表日志信息
            if(PublicUtil.isNotEmpty(followUpDto.getRemark())){
                omcFollowUpLog.setRemark(followUpDto.getRemark());
            }
            if(PublicUtil.isNotEmpty(followUpDto.getIntention())){
                omcFollowUpLog.setIntention(followUpDto.getIntention());
            }


            omcFollowUpLog.setFollowUpDate(new Date());
            omcFollowUpLog.setReviewStaff(getLoginAuthDto().getUserName());
            omcFollowUpLog.setFollowUpId(omcFollowUp.getId());

            int save = omcFollowUpLogService.save(omcFollowUpLog);
            if(save>0){

                int update = omcFollowUpService.update(omcFollowUp);

                return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
            }else {

                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("FollowUpLog.error", null));
            }

        }



        return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("FollowUp.error", null));

    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "搜索回访跟进")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody @Valid  FollowUpSelectDto follow, BindingResult br) {

        // 获取当前用户角色
        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(getLoginAuthDto().getUserId());

        //如果角色是销售员,那么只能看自己录入的信息
        for (RpcRoleVo rpcRoleVo : roleVoList) {
            String roleCode = rpcRoleVo.getRoleCode();
            if(roleCode.equals(OldInConstant.SALESMAN)){
                follow.setUserId(getLoginAuthDto().getUserId());
            }
        }

        follow.setGroupId(getLoginAuthDto().getGroupId());

        List<FollowUpVo> list=omcFollowUpService.selectFollowUp(follow);


            for (FollowUpVo followUpVo : list) {
                if(!followUpVo.getUserId().equals(getLoginAuthDto().getUserId())){
                    followUpVo.setIsMy(1);
                }
            }


        PageInfo<FollowUpVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查看跟进记录")
    @RequestMapping(value = "/selectLogById", method = RequestMethod.POST)
    public Wrapper selectLogById(Long id) {

        OmcFollowUpLog followUpLog = new OmcFollowUpLog();
        followUpLog.setFollowUpId(id);
        List<OmcFollowUpLog> omcFollowUpLogs = omcFollowUpLogService.select(followUpLog);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),omcFollowUpLogs);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "转移客户")
    @RequestMapping(value = "/shift", method = RequestMethod.POST)
    public Wrapper shift(@RequestBody @Valid FollowUpShiftDto followUpShiftDto) {

        // 获取当前用户角色
        List<RpcRoleVo> roleVoList = uacRpcService.queryRoleListByUserId(getLoginAuthDto().getUserId());

        //如果角色是销售员,那么只能看自己录入的信息
        int a=0;
        for (RpcRoleVo rpcRoleVo : roleVoList) {
            String roleCode = rpcRoleVo.getRoleCode();
            if(roleCode.equals(OldInConstant.SALES_MANAGER)){
                a++;
            }
        }
        if(a<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("followUp.shift.error", null));
        }


        Integer result= omcFollowUpService.followUpShift(followUpShiftDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


}
